/*
 * @Descripttion: 
 * @version: 
 * @Author: turingchang
 * @Date: 2020-08-24 20:29:35
 * @LastEditors: turingchang
 * @LastEditTime: 2020-08-25 17:52:19
 */

//时间相关辅助类
const moment = require('moment');


var time = {};

time.moment = moment;

time.getTimeSpan = function () {
    return (new Date()).getTime();
}

/**
 * 将毫秒转化为具体时间字符串=>x天x小时x分钟x秒
 * @param {number} ms 
 * @return {string} 转换后的时间字符串=>x天x小时x分钟x秒
 */
time.convertTimeStr = function (ms) {
    if (!ms) {
        return 0;
    }
    var second = 1000;
    var minute = second * 60;
    var hour = minute * 60;
    var day = 24 * hour;
    var time = '';

    if (ms < second) {
        return time += parseInt(ms) + '毫秒';
    }

    if (ms >= day) {
        time += parseInt(ms / day) + '天';
        ms %= 24 * 3600 * 1000;
    }
    if (ms >= hour) {
        time += parseInt(ms / hour) + '小时';
        ms %= 3600 * 1000;
    }
    if (ms >= minute) {
        time += parseInt(ms / minute) + '分钟';
        ms %= 60 * 1000;
    }
    if (ms >= second) {
        time += parseInt(ms / second) + '秒';
    }

    return time.trim();
};


/**
 * 获取当天，本周，本月的开始时间和结束时间段
 * @param {string} t 当天：day, 本周：week,本月：month
 */
time.getCurrentPeriod = function (t) {
    if (t === 'day') {
        return {
            start_at: moment().startOf('day'),
            end_at: moment().endOf('day')
        }
    } else if (t === 'week') {
        //老外的星期是从周日开始算，所以我们的要加1
        return {
            start_at: moment().startOf('week').add(1, 'day'),
            end_at: moment().endOf('week').add(1, 'day')
        };
    } else if (t === 'month') {
        return {
            start_at: moment().startOf('month'),
            end_at: moment().endOf('month')
        }
    }
    return null;
};

/**
 * 时间字符串转换为时间戳
 * @param {string} s 时间字符串
 */
time.dateToTimeStamp = function (s) {
    return Date.parse(s.replace(/-/gi, "/"));
}

/**
 * 将时间格式化为几分钟前、几小时前等
 * @param {string} s 时间。如2020-08-24 23:36:10
 */
time.formatMoment = function (s) {
    var publishTime = this.dateToTimeStamp(s) / 1000,
        d_seconds,
        d_minutes,
        d_hours,
        d_days,
        timeNow = parseInt(new Date().getTime() / 1000),
        d,

        date = new Date(publishTime * 1000),
        Y = date.getFullYear(),
        M = date.getMonth() + 1,
        D = date.getDate(),
        H = date.getHours(),
        m = date.getMinutes(),
        s = date.getSeconds();
    //小于10的在前面补0
    if (M < 10) {
        M = '0' + M;
    }
    if (D < 10) {
        D = '0' + D;
    }
    if (H < 10) {
        H = '0' + H;
    }
    if (m < 10) {
        m = '0' + m;
    }
    if (s < 10) {
        s = '0' + s;
    }

    d = timeNow - publishTime;
    d_days = parseInt(d / 86400);
    d_hours = parseInt(d / 3600);
    d_minutes = parseInt(d / 60);
    d_seconds = parseInt(d);

    if (d_days > 0 && d_days < 3) {
        return d_days + '天前';
    } else if (d_days <= 0 && d_hours > 0) {
        return d_hours + '小时前';
    } else if (d_hours <= 0 && d_minutes > 0) {
        return d_minutes + '分钟前';
    } else if (d_seconds < 60) {
        if (d_seconds <= 0) {
            return '刚刚';
        } else {
            return d_seconds + '秒前';
        }
    } else if (d_days >= 3 && d_days < 30) {
        return M + '-' + D + ' ' + H + ':' + m;
    } else if (d_days >= 30) {
        return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
    }
}

time.toDate = function (d) {
    d = d.substring(0, 19);
    d = d.replace(/-/g, '/');
    var timestamp = new Date(d).getTime();
    // 根据毫秒数构建 Date 对象
    var date = new Date(timestamp);
    // 格式化日期
    var dateTime = date.toLocaleString();
    return dateTime;
};

module.exports = time;